//
//  LunchScreenViewController.m
//  ZhihunewPaper
//
//  Created by 梅杰 on 16/7/23.
//  Copyright © 2016年 梅杰. All rights reserved.
//

#import "LunchScreenViewController.h"
#import "MainViewController.h"
#import "MMDrawerController.h"
#import "AppDelegate.h"
#import "HttpsManager.h"
@interface LunchScreenViewController ()

@property (weak, nonatomic) UIImageView *imageView;
@property (weak, nonatomic) UIImageView *launchView;
@property (weak, nonatomic) UILabel *titleLab;
@end

@implementation LunchScreenViewController

- (void)loadView{
    [super loadView];
    UIImage *image = [UIImage imageNamed:@"Default"];

    UIImageView *launchView = [[UIImageView alloc] initWithImage:image];
    launchView.frame = self.view.frame;
    self.launchView = launchView;
    [self.view addSubview:launchView];
    
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.frame = self.view.frame;
    self.imageView = imageView;
    [self.view addSubview:imageView];
    
    UILabel *titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, KScreenHeight - 50, KScreenWidth, 50)];
    self.titleLab = titleLbl;
    [self.view addSubview:titleLbl];
    
    titleLbl.font = [UIFont systemFontOfSize:14];
    titleLbl.textColor = [UIColor lightGrayColor];
    titleLbl.textAlignment = NSTextAlignmentCenter;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self showStartImage];
}
- (void) getMainVC {
    MainViewController *mainVC = [[MainViewController alloc] init];
    UIViewController *drawViewVC = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"DrawerVC"];
    UINavigationController *mainNAVC = [[UINavigationController alloc] initWithRootViewController:mainVC];
    //    mainNAVC.delegate = mainVC;
    MMDrawerController *rootVC = [[MMDrawerController alloc] initWithCenterViewController:mainNAVC leftDrawerViewController:drawViewVC];
    rootVC.showsShadow = YES;
    AppDelegate *delegate = [UIApplication sharedApplication].delegate;
    delegate.mmDrawer = rootVC;
    
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
    [rootVC setMaximumLeftDrawerWidth:screenWidth - 120 animated:YES completion:nil];
    [rootVC setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeAll];
    [rootVC setCloseDrawerGestureModeMask:MMCloseDrawerGestureModeAll];
    
    self.view.window.rootViewController = rootVC;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)showStartImage {
    
    CGFloat scale = [UIScreen mainScreen].scale;
    NSInteger imageWidth = [@(KScreenWidth * scale) integerValue];
    NSInteger imageHeight = [@(KScreenHeight * scale) integerValue];
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"interface.plist" ofType:nil]];
    NSString *imageURL = [NSString stringWithFormat:@"%@/%ld*%ld",dict[@"launchImage"],imageWidth,imageHeight];

    [HttpsManager getUrlDataWithUrl:imageURL Param:nil Success:^(NSDictionary * _Nonnull dict, Boolean flag) {
        NSDictionary *urlDataDict = ((NSArray *)dict[@"creatives"]).firstObject;
        NSString *imageURL = (NSString *)[urlDataDict objectForKey:@"url"];
        NSString *title = urlDataDict[@"text"];
        UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]]];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            _imageView.image = image;
            _titleLab.text = title;
            [UIView animateWithDuration:1.5f delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
                _launchView.alpha = 0.f;
                _imageView.transform = CGAffineTransformMakeScale(1.08, 1.08);
            } completion:^(BOOL finished) {
                [self getMainVC];
            }];
        });
    } fail:^(NSError * _Nonnull error) {
        NSLog(@"启动图获取失败 %@",[error localizedDescription]);
        [self getMainVC];
    }];
//     {
//        NSString *imageURL = responseObject[@"img"];
//        NSString *title = responseObject[@"text"];
//        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//            UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]]];
//            dispatch_async(dispatch_get_main_queue(), ^{
//                _imageView.image = image;
//                _titleLab.text = title;
//                [UIView animateWithDuration:1.5f delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
//                    _launchView.alpha = 0.f;
//                    _imageView.transform = CGAffineTransformMakeScale(1.08, 1.08);
//                } completion:^(BOOL finished) {
//                    self.view.window.rootViewController = ((AppDelegate *)[UIApplication sharedApplication].delegate).mainViewController;
//                }];
//            });
//        });
//    } failure:^(NSError *error) {
//        NSLog(@"%@",error);
//    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
